var page = 1;
var cpp = 9; //categories per page
var cc = $('.category_image_wrapper').length; // category count
var pages = Math.ceil(cc / cpp);
var pageWidth = $('#category_container').width() + 25;
var selected_categories = new Array(); //ids of selected categories
var categories_needed = 2;

$(function(){
    

    // show next page of categories
    $(".pagination .more").bind('click', function() {
        if (page < pages) {
            margin = -1 * page * pageWidth;
            $("#category_images").animate({marginLeft:margin}, 1000);
            page = page + 1;
        } 

        if (page == pages) {
            $(".pagination .more").css('color','#ccc');
        } else {
            $(".pagination .back").show();
        }
        return false; 
    });

    // show previous page of categories
    $(".pagination .back").bind('click', function() {
        if (page > 1) {
            page = page - 1;
            margin = -1 * (page - 1) * pageWidth;
            $("#category_images").animate({marginLeft:margin}, 1000);
        }

        if (page == 1){
            $(".pagination .back").hide();
        } else {
            $(".pagination .more").css('color','');
        }
        return false; 
    });

    $(".category_image").bind('click', function() {
        id = $(this).attr('id');
        if(is_selected(id))
            remove_selection(id);
        else
            add_selection(id);
        return false;
    });

    $('#clear_selected').bind('click', function() {
        for (i=0; i < selected_categories.length; i++) {
            $('#'+selected_categories[i]).removeClass('selected');
        }
        selected_categories.length = 0;
        update_selection();
        
        return false;
    });

    $('#selection_done').bind('click', function() {
        if(selected_categories.length == 2)
            window.location = 'selection.php?selected_categories=' + selected_categories.join(':'); 
    });
});

function is_selected(id) {
    for (i=0; i < selected_categories.length; i++) {
        if (selected_categories[i] == id) {
            return true;
        }
    }
    return false;
}

function remove_selection(id) {
    selected_categories.remove(id);
    $('#'+id).removeClass('selected');
    update_selection();
}

function add_selection(id) {
    if (selected_categories.length < categories_needed) {
        selected_categories.push(id);
        $('#'+id).addClass('selected');
        update_selection();
    }
}

function update_selection() {
    $('#category_selected ol').empty();
    for (i=0; i < selected_categories.length; i++) {
        id = selected_categories[i];
        text = findcat(id).name;
        $('#category_selected ol').append('<li>' + text + '</li>');
    }
    var count = selected_categories.length;
    if(count == 2) {
        $('#selection_done').removeClass('disabled');
        $('#description').text('');
    } else {
        $('#selection_done').addClass('disabled');
        if(count == 1)
            $('#description').text('Select 1 more category');
        else
            $('#description').text('Select 2 categories');
    }

    $('#selected_count').text(selected_categories.length);
    $('#total_count').text(categories_needed);
}

function findcat(id) {
    for (var i=0; i<categories.length; i++) {
        if (categories[i].id == id) {
            return categories[i];
        }
    }
}

Array.prototype.remove = function(id) {
   for (i=0; i < selected_categories.length; i++) {
        if (selected_categories[i] == id) {
            selected_categories.splice(i,1);
        }
    } 
}
